package com.lanqiao.controller;

import com.lanqiao.service.IUserService;
import com.lanqiao.vo.UsersInfo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2018/10/4.
 */
@Controller
@RequestMapping("/user")
@SessionAttributes({"logUser"})
public class UserController {

    static Logger logger = Logger.getLogger(UserController.class);

    @Resource
    IUserService userService;

    /**
     * @param user
     * @param map
     * @return
     */
    @RequestMapping("/login")
    public String login(UsersInfo user, ModelMap map){
        logger.debug("登录：" + user);
        UsersInfo logUser = userService.login(user);
        if (logUser!=null) {
            map.addAttribute("logUser", logUser);
            return "redirect:/house/search";
        }else {
            return "redirect:/login_form.jsp";
        }
    }

    @RequestMapping("/regist")
    public String regist(UsersInfo user){
        logger.debug("注册：" + user);
        int logUser = userService.insert(user);
        if (logUser==1) {
            return "redirect:/login_form.jsp";
        }

        // 如果在一个方法处理完后接着交其它方法继续处理可以返回这样的字符串
        // return "redirect:/user/login"; "forward:/user/login"
        return "redirect:/reg.jsp?flag="+logUser;
    }

    @RequestMapping("/logOff")
    public String userLogOff(HttpSession session, SessionStatus sessionStatus){
        session.invalidate();
        sessionStatus.setComplete();
        return "redirect:/login_form.jsp";
    }
}
